package com.giw.uima.makers;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.resource.ResourceInitializationException;

public class FindAllSurnames {
	private final static String DICT_NAME_PATH="src/main/resources/dictionaries/dictionary_name.txt";
	private final static String DICT_SURNAME_PATH="src/main/resources/dictionaries/dictionary_surname.txt";
	private final static String DICT_COMPET_PATH="src/main/resources/dictionaries/dictionary_competition.txt";
	private final static String DICT_TEAM_PATH="src/main/resources/dictionaries/dictionary_team.txt";
	private List<String> namesList;
	private List<String> surnamesList;
	private List<String> documents_path;
	private List<String> teams;
	private List<String> competitions;
	
	public FindAllSurnames(){
		this.namesList=new LinkedList<String>();
		this.surnamesList=new LinkedList<String>();
		this.teams = new LinkedList<String>();
		this.competitions = new LinkedList<String>();
		this.documents_path=new LinkedList<String>();
		this.documents_path.add("src/main/resources/dictionaries/filefinale.txt");
		try{ 
			FileReader f = new FileReader(DICT_NAME_PATH);; 
			BufferedReader in = new BufferedReader(f);  
			String line=in.readLine(); 
			while(line!=null){
				this.namesList.add(line);
				line=in.readLine();
			}
			f.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		Collections.sort(namesList);
		try{ 
			FileReader f = new FileReader(DICT_SURNAME_PATH);; 
			BufferedReader in = new BufferedReader(f);  
			String line=in.readLine(); 
			while(line!=null){
				this.surnamesList.add(line);
				line=in.readLine();
			}
			f.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		Collections.sort(surnamesList);
		try{ 
			FileReader f = new FileReader(DICT_TEAM_PATH);
			BufferedReader in = new BufferedReader(f);  
			String line=in.readLine(); 
			while(line!=null){
				this.teams.add(line);
				line=in.readLine();
			}
			f.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		Collections.sort(teams);
		try{ 
			FileReader f = new FileReader(DICT_COMPET_PATH);; 
			BufferedReader in = new BufferedReader(f);  
			String line=in.readLine(); 
			while(line!=null){
				this.competitions.add(line);
				line=in.readLine();
			}
			f.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		Collections.sort(competitions);
	}
	
	public void find_all_surnames() throws ResourceInitializationException, 
	AnalysisEngineProcessException{
		String text;
		SurnameIdentifier sIdentifier;
		for(String path:this.documents_path){
			int i=0;
			while((text=this.getNextDocument(path, i))!=null){
				Runtime.getRuntime().gc();
				System.out.println("Document: "+text.substring(0,9));
				sIdentifier=new SurnameIdentifier();
				this.surnamesList=sIdentifier.find_surname(text, this.surnamesList,this.namesList,this.teams,this.competitions);
				sIdentifier=null;
				Runtime.getRuntime().gc();
				i++;
			}
		}

	}
	
	private String getNextDocument(String path,int i){
		String line=null;
		BufferedReader in=null;
		try{
			FileReader f = new FileReader(path);; 
			in = new BufferedReader(f);
			if(i==0){
				return in.readLine();
			}
			int j=0;
			while(j<i){
				in.readLine();
				j++;
			}
			line=in.readLine();
			in.close();
			f.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return line;
	}
	
	public static void main(String []args) throws 
	AnalysisEngineProcessException, ResourceInitializationException{
		FindAllSurnames fas=new FindAllSurnames();
		fas.find_all_surnames();
	}

}
